package com.example.ex_menu;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.SubMenu;

public class MenuSUB extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_sub);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){

        SubMenu sub = menu.addSubMenu("搜索");
        sub.setIcon(android.R.drawable.ic_menu_search);
        sub.add(0,1,1, "本地");
        sub.add(0,2,2, "网络");

        SubMenu sub1 = menu.addSubMenu("跳转");
        sub1.setIcon(android.R.drawable.ic_menu_search);
        sub1.add(0,3,3, "跳转到MenuActivity");
        sub1.add(0,4,4, "跳转到MenuContext");
        sub1.add(0,5,5, "返回上一个活动");

        return true;    //返回值为true时，能正常实现点击submenu的选项可以正常实现活动跳转
        //return super.onCreateOptionsMenu(menu);   //使用该返回值时，但点击submenu的选项时，该活动出现闪退现象
    }

    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item){
        switch (item.getItemId()){
            case 3:
                Intent intent = new Intent(MenuSUB.this,MenuActivity.class);
                startActivity(intent);
                break;
            case 4:
                Intent intent1 = new Intent(MenuSUB.this,MenuContext.class);
                startActivity(intent1);
                break;
            case 5:
                finish();
                break;
        }

        //该方法的返回值与onCreateOptionsMenu()方法的返回值一样
        return true;
        //return onOptionsItemSelected(item);
    }
}
